import numpy as np
import pandas as pd
import geopandas as gpd
import os
from matplotlib import pyplot as plt
data_path ='/home/tandonsa/PycharmProjects/github_sam991/Leveraging-GPS-Data-for-Machine-Learning/Data/4+-+Data+Vizualization/data'
small_areas = gpd.read_file(os.path.join(data_path,'malmo-pop.shp'))
small_areas.head()
| fid | Deso | Age_5 | Age6_9 | Age10_15 | Age16_19 | Age20_24 | Age25_29 | Age30_34 | Age35_39 | ... | Age45_49 | Age50_54 | Age55_59 | Age60_64 | Age65_69 | Age70_74 | Age75_79 | Age80_w | Total | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2822.0 | 1280C1590 | 125.0 | 82.0 | 113.0 | 83.0 | 80.0 | 104.0 | 99.0 | 109.0 | ... | 99.0 | 115.0 | 88.0 | 93.0 | 101.0 | 112.0 | 141.0 | 410.0 | 2057.0 | POLYGON ((373380.287 6160661.365, 373382.480 6... |
| 1 | 2823.0 | 1280C1430 | 332.0 | 201.0 | 262.0 | 141.0 | 165.0 | 217.0 | 212.0 | 195.0 | ... | 140.0 | 139.0 | 140.0 | 101.0 | 75.0 | 54.0 | 39.0 | 35.0 | 2616.0 | POLYGON ((372667.079 6159991.971, 372653.152 6... |
| 2 | 2824.0 | 1280C1220 | 128.0 | 69.0 | 65.0 | 34.0 | 93.0 | 132.0 | 135.0 | 109.0 | ... | 71.0 | 91.0 | 91.0 | 119.0 | 153.0 | 110.0 | 92.0 | 122.0 | 1696.0 | POLYGON ((372812.733 6158498.565, 372817.194 6... |
| 3 | 2825.0 | 1280C1490 | 76.0 | 70.0 | 118.0 | 92.0 | 102.0 | 57.0 | 48.0 | 89.0 | ... | 108.0 | 101.0 | 94.0 | 80.0 | 59.0 | 62.0 | 35.0 | 62.0 | 1340.0 | POLYGON ((374168.622 6160181.233, 374169.100 6... |
| 4 | 2826.0 | 1280C1530 | 220.0 | 94.0 | 118.0 | 74.0 | 150.0 | 200.0 | 196.0 | 163.0 | ... | 113.0 | 81.0 | 107.0 | 92.0 | 77.0 | 45.0 | 31.0 | 32.0 | 1928.0 | POLYGON ((375219.802 6160462.505, 375274.357 6... |
5 rows × 21 columns
small_areas.crs
<Projected CRS: PROJCS["SWEREF99_TM",GEOGCS["GCS_SWEREF99",DATUM[" ...> Name: SWEREF99 TM Axis Info [cartesian]: - [east]: Easting (metre) - [north]: Northing (metre) Area of Use: - undefined Coordinate Operation: - name: UTM zone 33N - method: Transverse Mercator Datum: SWEREF99 - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
print(small_areas.shape)
small_areas.plot(figsize=(10,9),color='blue',alpha=0.5,edgecolor="#CFAC84")
(202, 21)
<AxesSubplot:>
roads = gpd.read_file(os.path.join(data_path,"malmo-streets.shp"))
roads.head()
| osm_id | name | highway | waterway | aerialway | barrier | man_made | z_order | other_tags | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 23440666 | Blekingsborgsgatan | residential | None | None | None | None | 3 | None | LINESTRING (374183.208 6161017.321, 374184.590... |
| 1 | 23618297 | ÃrtholmsvÃĪgen | residential | None | None | None | None | 3 | None | LINESTRING (373860.815 6160562.585, 373773.040... |
| 2 | 28552693 | Kransgatan | residential | None | None | None | None | 3 | None | LINESTRING (373648.221 6160291.937, 373678.893... |
| 3 | 28552694 | TÃķrngatan | residential | None | None | None | None | 3 | None | LINESTRING (373520.164 6160333.786, 373548.277... |
| 4 | 28552695 | SÃķdertorpsvÃĪgen | residential | None | None | None | None | 3 | None | LINESTRING (373340.961 6160304.145, 373370.603... |
roads.crs
<Projected CRS: PROJCS["SWEREF99_TM",GEOGCS["GCS_SWEREF99",DATUM[" ...> Name: SWEREF99 TM Axis Info [cartesian]: - [east]: Easting (metre) - [north]: Northing (metre) Area of Use: - undefined Coordinate Operation: - name: UTM zone 33N - method: Transverse Mercator Datum: SWEREF99 - Ellipsoid: GRS 1980 - Prime Meridian: Greenwich
print(roads.shape)
roads.plot(figsize=(5,7),edgecolor="#CFAC84",alpha=0.8)
(24551, 10)
<AxesSubplot:>
preschools = gpd.read_file(os.path.join(data_path,'preschools.shp'))
preschools.head()
| BESÖKSADR | BESÖKSORT | FIRMABEN | FÖRETAGSN | KOM | KOMTEXT | POSTADRE00 | POSTADRESS | POSTNUMMER | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | VIKINGAGATAN 60 | MALMÖ | FÖRSKOLA | MALMÖ MONTESSORISKOL | 1280 | Malmö | VIKINGAGATAN 60 | MALMÖ | 21774 | POINT (371044.000 6162502.000) |
| 1 | KAMRERGATAN 11 | MALMÖ | None | JUDISKA FÖRSKOLAN CH | 1280 | Malmö | KAMRERGATAN 11 | MALMÖ | 21156 | POINT (374769.000 6163245.000) |
| 2 | VANÅSGATAN 66 | MALMÖ | None | RUMÄNSKA ORTODOXA FÖ | 1280 | Malmö | VANÅSGATAN 66 | MALMÖ | 21620 | POINT (371788.000 6161201.000) |
| 3 | GRÖNVÄGEN 6 | ARLÖV | FÖRSKOLA GRÖNEBO | BURLÖVS KOMMUN | 1231 | Burlöv | BOX 53 | ARLÖV | 23221 | POINT (379012.000 6166772.000) |
| 4 | KORNVÄGEN 1 | ARLÖV | FÖRSKOLA SVENSHÖG | BURLÖVS KOMMUN | 1231 | Burlöv | BOX 53 | ARLÖV | 23221 | POINT (379450.000 6167390.000) |
preschools.plot()
<AxesSubplot:>
fig,ax = plt.subplots(figsize=(9,11))
small_areas.plot(ax=ax,color="#F1ECEB",alpha=0.7)
preschools.plot(ax=ax,marker='+',color='black',edgecolor='white',alpha=0.8)
roads.plot(ax=ax,color='grey',alpha=0.1)
plt.title("Malmo Neighbourhoods",fontsize=40,color='grey')
plt.show()
/home/tandonsa/anaconda3/envs/pygeo/lib/python3.7/site-packages/geopandas/plotting.py:306: UserWarning: You passed a edgecolor/edgecolors ('white') for an unfilled marker ('+'). Matplotlib is ignoring the edgecolor in favor of the facecolor. This behavior may change in the future.
collection = ax.scatter(x, y, vmin=vmin, vmax=vmax, cmap=cmap, **kwargs)
Visualize density
small_areas.columns
Index(['fid', 'Deso', 'Age_5', 'Age6_9', 'Age10_15', 'Age16_19', 'Age20_24',
'Age25_29', 'Age30_34', 'Age35_39', 'Age40_44', 'Age45_49', 'Age50_54',
'Age55_59', 'Age60_64', 'Age65_69', 'Age70_74', 'Age75_79', 'Age80_w',
'Total', 'geometry'],
dtype='object')
small_areas.plot(column='Age80_w',figsize=(12,10)) # choropleth maps
<AxesSubplot:>
small_areas.plot(column='Age80_w',figsize=(12,10) ,cmap='Blues',scheme='quantiles',k=6,legend=True) # choropleth maps
<AxesSubplot:>
The right way to show th crololeth maps is to normalize the data first
small_areas['Age80_perc']=(small_areas['Age80_w']/small_areas["Total"])*100
small_areas["Age80_perc"]
0 19.931940
1 1.337920
2 7.193396
3 4.626866
4 1.659751
...
197 1.081359
198 6.577225
199 0.809353
200 8.840864
201 2.761104
Name: Age80_perc, Length: 202, dtype: float64
fig,ax = plt.subplots(figsize=(12,10))
small_areas.plot(column='Age80_perc',cmap='Blues',scheme='quantiles',k=6,legend=True,ax=ax)
<AxesSubplot:>
fig,ax = plt.subplots(1,2,figsize=(20,18))
small_areas.plot(column='Age80_perc',cmap='Blues',scheme='quantiles',k=6,ax=ax[0],legend=True)
ax[0].set_title("Normalized Choropleth Map")
small_areas.plot(column='Age80_w',cmap='Blues',scheme='quantiles',k=6,ax=ax[1],legend=True)
ax[0].set_title("Raw data Choropleth Map")
Text(0.5, 1.0, 'Raw data Choropleth Map')
Simply give column name in the marker size
small_areas_points = small_areas.copy()
small_areas_points['geometry'] = small_areas_points['geometry'].centroid
small_areas_points.head()
| fid | Deso | Age_5 | Age6_9 | Age10_15 | Age16_19 | Age20_24 | Age25_29 | Age30_34 | Age35_39 | ... | Age50_54 | Age55_59 | Age60_64 | Age65_69 | Age70_74 | Age75_79 | Age80_w | Total | geometry | Age80_perc | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2822.0 | 1280C1590 | 125.0 | 82.0 | 113.0 | 83.0 | 80.0 | 104.0 | 99.0 | 109.0 | ... | 115.0 | 88.0 | 93.0 | 101.0 | 112.0 | 141.0 | 410.0 | 2057.0 | POINT (373736.130 6160844.324) | 19.931940 |
| 1 | 2823.0 | 1280C1430 | 332.0 | 201.0 | 262.0 | 141.0 | 165.0 | 217.0 | 212.0 | 195.0 | ... | 139.0 | 140.0 | 101.0 | 75.0 | 54.0 | 39.0 | 35.0 | 2616.0 | POINT (372932.457 6160165.211) | 1.337920 |
| 2 | 2824.0 | 1280C1220 | 128.0 | 69.0 | 65.0 | 34.0 | 93.0 | 132.0 | 135.0 | 109.0 | ... | 91.0 | 91.0 | 119.0 | 153.0 | 110.0 | 92.0 | 122.0 | 1696.0 | POINT (373183.207 6158866.154) | 7.193396 |
| 3 | 2825.0 | 1280C1490 | 76.0 | 70.0 | 118.0 | 92.0 | 102.0 | 57.0 | 48.0 | 89.0 | ... | 101.0 | 94.0 | 80.0 | 59.0 | 62.0 | 35.0 | 62.0 | 1340.0 | POINT (374515.204 6160556.319) | 4.626866 |
| 4 | 2826.0 | 1280C1530 | 220.0 | 94.0 | 118.0 | 74.0 | 150.0 | 200.0 | 196.0 | 163.0 | ... | 81.0 | 107.0 | 92.0 | 77.0 | 45.0 | 31.0 | 32.0 | 1928.0 | POINT (375533.769 6160531.143) | 1.659751 |
5 rows × 22 columns
small_areas_points.plot(color='grey')
<AxesSubplot:>
# to plot bubble plot , we need to add column name in marker size
fig,ax = plt.subplots(figsize=(8,10))
small_areas_points.plot(ax=ax,color='grey',markersize="Age80_w")
plt.axis('equal')
plt.show()
# to plot bubble plot , we need to add column name in marker size
fig,ax = plt.subplots(figsize=(8,10))
small_areas.plot(ax=ax,color="lightgrey",alpha=0.7,edgecolor='grey',linewidth=0.4)
small_areas_points.plot(ax=ax,color="#07424A",markersize="Age80_w",categorical=False,legend=True)
plt.axis('equal')
plt.show()
from matplotlib import pyplot as plt
import geopandas as gpd
import folium
import os
countries = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
data_path = '/home/tandonsa/PycharmProjects/github_sam991/Leveraging-GPS-Data-for-Machine-Learning/Data/5-+Interactive+Visualization/data'
# conflicts_dataset points
confilct_path = os.path.join(data_path,'acled2019.shp')
acled = gpd.read_file(confilct_path)
acled.head()
| data_id | iso | event_id_c | event_id_n | event_date | year | time_preci | event_type | sub_event_ | actor1 | ... | latitude | longitude | geo_precis | source | source_sca | notes | fatalities | timestamp | iso3 | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 6768128 | 887 | YEM45982 | 45982 | 31 December 2019 | 2019 | 1 | Battles | Armed clash | Military Forces of Yemen (2012-) | ... | 14.3541 | 47.0765 | 2 | Yemen Data Project; Aden al Ghad; Al Janoob al... | Local partner-National | On 31 December 2019, clashes reportedly erupte... | 0 | 1580761030 | YEM | POINT (47.07650 14.35410) |
| 1 | 6768129 | 887 | YEM45983 | 45983 | 31 December 2019 | 2019 | 1 | Strategic developments | Change to group/activity | Military Forces of Yemen (2012-) Security Belt... | ... | 14.3541 | 47.0765 | 2 | Al Janoob al Youm; Yemen Data Project | Local partner-National | On 31 December 2019, forces loyal to the South... | 0 | 1580761030 | YEM | POINT (47.07650 14.35410) |
| 2 | 6768130 | 887 | YEM45984 | 45984 | 31 December 2019 | 2019 | 1 | Battles | Armed clash | Military Forces of Yemen (2012-) | ... | 14.2081 | 47.1570 | 1 | Al Janoob al Youm; Aden al Ghad; Yemen Data Pr... | Local partner-National | On 31 December 2019, clashes reportedly erupte... | 1 | 1580761030 | YEM | POINT (47.15700 14.20810) |
| 3 | 6715395 | 356 | IND68948 | 68948 | 31 December 2019 | 2019 | 1 | Protests | Peaceful protest | Protesters (India) | ... | 20.2724 | 85.8338 | 1 | Pioneer (India) | National | On 31 December, students' wing of INC and Mili... | 0 | 1578514389 | IND | POINT (85.83380 20.27240) |
| 4 | 6943236 | 222 | SLV861 | 861 | 31 December 2019 | 2019 | 1 | Violence against civilians | Attack | B-18: Barrio-18 | ... | 13.5894 | -89.2383 | 1 | El Salvador.com | National | On 31th December 2019 in Huizucar (La Libertad... | 1 | 1582839958 | SLV | POINT (-89.23830 13.58940) |
5 rows × 32 columns
print("Latitude: ",acled.latitude.min())
print("Longitude:",acled.longitude.max())
Latitude: -54.9244 Longitude: 158.6541
m = folium.Map(location=[53.431085,-2.960422])
m
# Zoom
m = folium.Map(location=[53.431085,-2.960422],zoom_start=15)
m
# change base map
m = folium.Map(location=[53.431085,-2.960422],zoom_start=15,tiles="CartoDB dark_matter")
m
folium.Marker(location=[53.431085,-2.960422],
popup='<i>Stanley Park</i>'
).add_to(m)
m
folium.Circle(location=[53.431085,-2.960422],
radius=25,
popup='Anfield Stadium',
color='crimson',
fill=True
).add_to(m)
m
# direct using Geojaons
m = folium.Map([4.546647,22.373178],zoom_start=2,tiles='cartodbpositron')
folium.GeoJson(acled.sample(1000)).add_to(m)
m
m = folium.Map([4.546647,22.373178],zoom_start=2,tiles='cartodbpositron')
acled_sample = acled.sample(1000)
locs_points = zip(acled_sample.geometry.y,acled_sample.geometry.x)
for location in locs_points:
folium.CircleMarker(location=location , color='red',radius=0.5).add_to(m)
m
from folium.plugins import FastMarkerCluster
cmap = folium.Map([4.546647,22.373178],zoom_start=2,tiles='cartodbpositron')
FastMarkerCluster(data=list(zip(acled_sample.geometry.y.values,acled_sample.geometry.x.values))).add_to(cmap)
folium.LayerControl().add_to(cmap)
cmap
from matplotlib import pyplot as plt
import geopandas as gpd
import numpy as np
import pandas as pd
import folium
import os
countries = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
data_path = '/home/tandonsa/PycharmProjects/github_sam991/Leveraging-GPS-Data-for-Machine-Learning/Data/5-+Interactive+Visualization/data'
# conflicts_dataset points
confilct_path = os.path.join(data_path,'acled2019.shp')
acled = gpd.read_file(confilct_path,mask=countries[countries["continent"]=='Africa'])
print(acled.shape)
acled.head()
(23641, 32)
| data_id | iso | event_id_c | event_id_n | event_date | year | time_preci | event_type | sub_event_ | actor1 | ... | latitude | longitude | geo_precis | source | source_sca | notes | fatalities | timestamp | iso3 | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 6714129 | 706 | SOM30213 | 30213 | 31 December 2019 | 2019 | 1 | Battles | Armed clash | Al Shabaab | ... | 2.3963 | 45.0159 | 1 | Undisclosed Source | Local partner-Other | 31 December 2019. In Warmahan, Al Shabaab mili... | 0 | 1578512393 | SOM | POINT (45.01590 2.39630) |
| 1 | 6723349 | 12 | ALG7951 | 7951 | 31 December 2019 | 2019 | 1 | Protests | Peaceful protest | Protesters (Algeria) | ... | 36.7525 | 3.0420 | 1 | Liberte (Algeria); TSA Algerie; El Watan (Alge... | National | On 31 December 2019, students held their 45th ... | 0 | 1579554007 | DZA | POINT (3.04200 36.75250) |
| 2 | 6723350 | 12 | ALG7953 | 7953 | 31 December 2019 | 2019 | 1 | Protests | Peaceful protest | Protesters (Algeria) | ... | 36.3650 | 6.6147 | 1 | Liberte (Algeria); El Watan (Algeria) | National | On 31 December 2019, students held their 45th ... | 0 | 1579554007 | DZA | POINT (6.61470 36.36500) |
| 3 | 6713368 | 404 | KEN7182 | 7182 | 31 December 2019 | 2019 | 1 | Violence against civilians | Attack | Police Forces of Kenya (2013-) | ... | -3.3543 | 40.0209 | 1 | Daily Nation (Kenya) | National | On December 31 2019, a suspected Al Shabaab re... | 1 | 1578512391 | KEN | POINT (40.02090 -3.35430) |
| 4 | 6718756 | 180 | DRC16324 | 16324 | 31 December 2019 | 2019 | 1 | Battles | Armed clash | Military Forces of Democratic Republic of Cong... | ... | -1.1869 | 29.4461 | 2 | Kivu Security Tracker | Local partner-Other | On 31 December 2019, the FARDC attacked Mayi M... | 0 | 1578943802 | COD | POINT (29.44610 -1.18690) |
5 rows × 32 columns
from folium.plugins import HeatMap, HeatMapWithTime
df = acled[['latitude','longitude']]
df.head()
| latitude | longitude | |
|---|---|---|
| 0 | 2.3963 | 45.0159 |
| 1 | 36.7525 | 3.0420 |
| 2 | 36.3650 | 6.6147 |
| 3 | -3.3543 | 40.0209 |
| 4 | -1.1869 | 29.4461 |
print("Latitude MAX {}, MIN {}".format(df['latitude'].max(),df['latitude'].min()))
print("Longitude MAX {}, MIN {}".format(df['longitude'].max(),df['longitude'].min()))
Latitude MAX 37.2744, MIN -34.4311 Longitude MAX 50.8255, MIN -17.4441
heat_data= list(df.values)
heat_data[:10]
[array([ 2.3963, 45.0159]), array([36.7525, 3.042 ]), array([36.365 , 6.6147]), array([-3.3543, 40.0209]), array([-1.1869, 29.4461]), array([34.9346, -2.7332]), array([12.9894, 7.6006]), array([-3.0451, 30.1592]), array([32.2463, -8.5294]), array([13.3877, 22.4228])]
m = folium.Map([4.546647,22.373178],zoom_start=2,tiles='cartodbpositron')
HeatMap(heat_data).add_to(m)## accepts data in form of list
m
gradient = {
'0.0':'blue',
'1':'red'
}
m = folium.Map([4.546647,22.373178],zoom_start=2,tiles='cartodbpositron')
HeatMap(heat_data,gradient=gradient).add_to(m)
m
gradient = {
'0':'Black',
'0.4':'Purple',
'0.6':'Red',
'0.8':'Yellow',
'1':'White'
}
m = folium.Map([4.546647,22.373178],zoom_start=2,tiles='cartodbpositron')
HeatMap(heat_data,gradient=gradient).add_to(m)
m
df_2019 = acled[acled['year']==2019]
df_2019_latlon = df_2019[['latitude','longitude','event_date']]
df_2019_latlon.head()
| latitude | longitude | event_date | |
|---|---|---|---|
| 0 | 2.3963 | 45.0159 | 31 December 2019 |
| 1 | 36.7525 | 3.0420 | 31 December 2019 |
| 2 | 36.3650 | 6.6147 | 31 December 2019 |
| 3 | -3.3543 | 40.0209 | 31 December 2019 |
| 4 | -1.1869 | 29.4461 | 31 December 2019 |
df_2019_latlon['heat'] = pd.to_datetime(df_2019_latlon['event_date'])
/home/tandonsa/anaconda3/envs/pygeo/lib/python3.7/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy """Entry point for launching an IPython kernel.
df_2019_latlon.head()
| latitude | longitude | event_date | heat | |
|---|---|---|---|---|
| 0 | 2.3963 | 45.0159 | 31 December 2019 | 2019-12-31 |
| 1 | 36.7525 | 3.0420 | 31 December 2019 | 2019-12-31 |
| 2 | 36.3650 | 6.6147 | 31 December 2019 | 2019-12-31 |
| 3 | -3.3543 | 40.0209 | 31 December 2019 | 2019-12-31 |
| 4 | -1.1869 | 29.4461 | 31 December 2019 | 2019-12-31 |
df_2019_latlon['month']= df_2019_latlon['heat'].dt.month
df_2019_latlon['date'] = df_2019_latlon['heat'].dt.date
data = df_2019_latlon[['latitude','longitude','month']]
data.sample(10)
/home/tandonsa/anaconda3/envs/pygeo/lib/python3.7/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy """Entry point for launching an IPython kernel. /home/tandonsa/anaconda3/envs/pygeo/lib/python3.7/site-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
| latitude | longitude | month | |
|---|---|---|---|
| 11174 | 0.4797 | 33.2344 | 7 |
| 7134 | -18.3998 | 47.8565 | 9 |
| 15252 | 32.2994 | -9.2372 | 5 |
| 21622 | 15.6365 | 32.5303 | 1 |
| 6074 | 35.6781 | 10.0963 | 9 |
| 17813 | 6.6550 | 6.3869 | 3 |
| 8656 | 4.3153 | 17.4669 | 8 |
| 16914 | 0.4833 | 42.7666 | 4 |
| 13586 | -5.9370 | 29.1785 | 5 |
| 17642 | 33.9119 | -5.4780 | 3 |
import seaborn as sns
sns.countplot(data['month'])
/home/tandonsa/anaconda3/envs/pygeo/lib/python3.7/site-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation. FutureWarning
<AxesSubplot:xlabel='month', ylabel='count'>
data_heat = [ [ [row['latitude'],row['longitude']] for index, row in data[data['month'] == i].iterrows()] for i in range(1,13)]
m = folium.Map([4.546647,22.373178],zoom_start=2,tiles='cartodbpositron')
HeatMapWithTime(data_heat,auto_play=True,max_opacity=0.8).add_to(m)## accepts data in form of list
m
from ipyleaflet import (Map,GeoData,basemaps,WidgetControl,GeoJSON,
LayersControl,Icon,Marker,basemap_to_tiles,
Choropleth,MarkerCluster,
Heatmap,SearchControl,FullScreenControl)
import os
import numpy as np
import pandas as pd
from ipywidgets import Text, HTML #
from branca.colormap import linear
import geopandas as gpd
import json
data_path = '/home/tandonsa/PycharmProjects/github_sam991/Leveraging-GPS-Data-for-Machine-Learning/Data/5-+Interactive+Visualization/data'
countries = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
asia_acled = gpd.read_file(os.path.join(data_path,'acled2019.shp'),mask=countries[countries['continent']=='Asia'])
asia_acled.head()
| data_id | iso | event_id_c | event_id_n | event_date | year | time_preci | event_type | sub_event_ | actor1 | ... | latitude | longitude | geo_precis | source | source_sca | notes | fatalities | timestamp | iso3 | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 6768128 | 887 | YEM45982 | 45982 | 31 December 2019 | 2019 | 1 | Battles | Armed clash | Military Forces of Yemen (2012-) | ... | 14.3541 | 47.0765 | 2 | Yemen Data Project; Aden al Ghad; Al Janoob al... | Local partner-National | On 31 December 2019, clashes reportedly erupte... | 0 | 1580761030 | YEM | POINT (47.07650 14.35410) |
| 1 | 6768129 | 887 | YEM45983 | 45983 | 31 December 2019 | 2019 | 1 | Strategic developments | Change to group/activity | Military Forces of Yemen (2012-) Security Belt... | ... | 14.3541 | 47.0765 | 2 | Al Janoob al Youm; Yemen Data Project | Local partner-National | On 31 December 2019, forces loyal to the South... | 0 | 1580761030 | YEM | POINT (47.07650 14.35410) |
| 2 | 6768130 | 887 | YEM45984 | 45984 | 31 December 2019 | 2019 | 1 | Battles | Armed clash | Military Forces of Yemen (2012-) | ... | 14.2081 | 47.1570 | 1 | Al Janoob al Youm; Aden al Ghad; Yemen Data Pr... | Local partner-National | On 31 December 2019, clashes reportedly erupte... | 1 | 1580761030 | YEM | POINT (47.15700 14.20810) |
| 3 | 6715395 | 356 | IND68948 | 68948 | 31 December 2019 | 2019 | 1 | Protests | Peaceful protest | Protesters (India) | ... | 20.2724 | 85.8338 | 1 | Pioneer (India) | National | On 31 December, students' wing of INC and Mili... | 0 | 1578514389 | IND | POINT (85.83380 20.27240) |
| 4 | 6714375 | 356 | IND68961 | 68961 | 31 December 2019 | 2019 | 1 | Protests | Peaceful protest | Protesters (India) | ... | 25.2905 | 79.8753 | 1 | Amar Ujala | Subnational | On Dec 31, protests were held by auto drivers ... | 0 | 1578514386 | IND | POINT (79.87530 25.29050) |
5 rows × 32 columns
centre = [57.71,11.98]
zoom= 12
m = Map(basemap = basemaps.Esri.WorldImagery,centre=centre,zoom=zoom)
stamen = basemap_to_tiles(basemaps.Stamen.Toner)
m.add_layer(stamen)
marker = Marker(location=centre,draggable=False)
m.add_layer(marker)
center = [4.546647,22.373178]
zoom = 3
map_style= {'color':'black','fillcolor':'#E0D071','opacity':0.03,'weight':1.9,'dashArray':'2'}
hover_style = {'fillcolor':'cyan','fillOpacity':0.4}
africa = countries[countries['continent']=='Africa']
africaMap = Map(basemap=basemaps.CartoDB.Positron,center=center,zoom=zoom)
geodata = GeoData(geo_dataframe=africa,style=map_style,hover_style=hover_style,name='Countries')
africaMap.add_layer(geodata)
africaMap
Map(center=[4.546647, 22.373178], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', …
html = HTML('Hover over Countris')
html.layout.margin = '0px 20px 20px 20px'
control = WidgetControl(widget=html,position='topright')
africaMap.add_control(control)
def update_html(feature, **kwargs):
html.value = '''
<h3><b>{}</b></h3>
<h4>GDP: {:.2e} people</h4>
<h4>Population: {}</h4>
'''.format(feature['properties']['name'],
feature['properties']['gdp_md_est'],
feature['properties']['pop_est'])
geodata.on_hover(update_html)
africa.head()
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
|---|---|---|---|---|---|---|
| 1 | 53950935 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... |
| 2 | 603253 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... |
| 11 | 83301151 | Africa | Dem. Rep. Congo | COD | 66010.0 | POLYGON ((29.34000 -4.49998, 29.51999 -5.41998... |
| 12 | 7531386 | Africa | Somalia | SOM | 4719.0 | POLYGON ((41.58513 -1.68325, 40.99300 -0.85829... |
| 13 | 47615739 | Africa | Kenya | KEN | 152700.0 | POLYGON ((39.20222 -4.67677, 37.76690 -3.67712... |
africa.at[60,'name']="Ivory Coast"
pop = dict(zip(africa['name'].tolist(), africa['pop_est'].tolist()))
africa[["name", "geometry"]].to_file("africa.json", driver="GeoJSON", encoding='utf-8')
geojson_data = json.load(open("africa.json",'r'))
for feature in geojson_data['features']:
properties = feature['properties']
feature.update(id=properties['name'])
#print(feature['id'])
choroplethData = Choropleth(
geo_data=geojson_data,
choro_data=pop,
colormap=linear.YlOrRd_04,
style={'fillOpacity': 0.8, 'dashArray': '5, 5'}
)
choro_map = Map(center = (4.546647, 22.373178), zoom = 3)
choro_map.add_layer(choroplethData)
choro_map
Map(center=[4.546647, 22.373178], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', …
import plotly.express as px
countries.head()
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
|---|---|---|---|---|---|---|
| 0 | 920938 | Oceania | Fiji | FJI | 8374.0 | MULTIPOLYGON (((180.00000 -16.06713, 180.00000... |
| 1 | 53950935 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... |
| 2 | 603253 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... |
| 3 | 35623680 | North America | Canada | CAN | 1674000.0 | MULTIPOLYGON (((-122.84000 49.00000, -122.9742... |
| 4 | 326625791 | North America | United States of America | USA | 18560000.0 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000... |
acled.head()
| data_id | iso | event_id_c | event_id_n | event_date | year | time_preci | event_type | sub_event_ | actor1 | ... | latitude | longitude | geo_precis | source | source_sca | notes | fatalities | timestamp | iso3 | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 6714129 | 706 | SOM30213 | 30213 | 31 December 2019 | 2019 | 1 | Battles | Armed clash | Al Shabaab | ... | 2.3963 | 45.0159 | 1 | Undisclosed Source | Local partner-Other | 31 December 2019. In Warmahan, Al Shabaab mili... | 0 | 1578512393 | SOM | POINT (45.01590 2.39630) |
| 1 | 6723349 | 12 | ALG7951 | 7951 | 31 December 2019 | 2019 | 1 | Protests | Peaceful protest | Protesters (Algeria) | ... | 36.7525 | 3.0420 | 1 | Liberte (Algeria); TSA Algerie; El Watan (Alge... | National | On 31 December 2019, students held their 45th ... | 0 | 1579554007 | DZA | POINT (3.04200 36.75250) |
| 2 | 6723350 | 12 | ALG7953 | 7953 | 31 December 2019 | 2019 | 1 | Protests | Peaceful protest | Protesters (Algeria) | ... | 36.3650 | 6.6147 | 1 | Liberte (Algeria); El Watan (Algeria) | National | On 31 December 2019, students held their 45th ... | 0 | 1579554007 | DZA | POINT (6.61470 36.36500) |
| 3 | 6713368 | 404 | KEN7182 | 7182 | 31 December 2019 | 2019 | 1 | Violence against civilians | Attack | Police Forces of Kenya (2013-) | ... | -3.3543 | 40.0209 | 1 | Daily Nation (Kenya) | National | On December 31 2019, a suspected Al Shabaab re... | 1 | 1578512391 | KEN | POINT (40.02090 -3.35430) |
| 4 | 6718756 | 180 | DRC16324 | 16324 | 31 December 2019 | 2019 | 1 | Battles | Armed clash | Military Forces of Democratic Republic of Cong... | ... | -1.1869 | 29.4461 | 2 | Kivu Security Tracker | Local partner-Other | On 31 December 2019, the FARDC attacked Mayi M... | 0 | 1578943802 | COD | POINT (29.44610 -1.18690) |
5 rows × 32 columns
px.scatter_geo(acled,lat='latitude',lon='longitude')
fig = px.scatter_mapbox(acled,lat='latitude',lon='longitude',zoom=1,mapbox_style='open-street-map')
fig.show()
fig = px.scatter_mapbox(
acled,lat='latitude',lon='longitude',color='event_type',size='fatalities',
color_continuous_scale=px.colors.cyclical.IceFire,
size_max=15,mapbox_style='carto-darkmatter',zoom=1
)
fig.show()
fig = px.scatter_mapbox(
acled,lat='latitude',lon='longitude',color='event_type',size='fatalities',
color_continuous_scale=px.colors.cyclical.IceFire,
size_max=15,mapbox_style='carto-darkmatter',zoom=1,animation_frame='event_date'
)
fig.show()
countries['area_size']=countries.area
countries['pop_density']=(countries.pop_est/countries.area_size)*100
/home/tandonsa/anaconda3/envs/pygeo/lib/python3.7/site-packages/ipykernel_launcher.py:1: UserWarning: Geometry is in a geographic CRS. Results from 'area' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.
px.choropleth(countries,locations='iso_a3',color='pop_density',hover_name='name')